using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;


public class MyBuildingTool : EditorWindow
{
    enum ControlState
    {
        ecs_None,
        ecs_Ground,
        ecs_BuildingWall,
        ecs_BuildingWoodenWall,
        ecs_Other,

        ecs_Del,
    }
    // init window
    static bool m_bInitialized = false;
    public static GameObject selObj;
    private static int m_nMapSize = 0;
    
    private static int m_eControlState = (int)ControlState.ecs_Ground;

    private static GroundEditor groundEditor = new GroundEditor();
    private static WallEditor wallEditor = new WallEditor();
    private static WoodenWallEditor woodenWallEditor = new WoodenWallEditor();
    private static OtherEditor otherEditor = new OtherEditor();
    
    private static int nStartX = 0;
    private static int nStartZ = 0;
    private static int nEndX = 0;
    private static int nEndZ = 0;
    
    [MenuItem("Window/" + "My Building Tool")]
    public static void Init()
    {
        selObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        selObj.transform.position = new Vector3(0,0,0);
        selObj.transform.localScale = new Vector3(1,0.1f,1);
		//给这个创建出来的对象起个名字
		selObj.name = "选中位置";

        SceneView.onSceneGUIDelegate += OnSceneGUI;
        
        groundEditor.Init();
        wallEditor.Init();
        woodenWallEditor.Init();
        otherEditor.Init();

        m_bInitialized = true;
    }
    void OnGUI()
    {
        if (!m_bInitialized)
        {
            return;
        }
        int nPreSize = m_nMapSize;
        // UI setting
        GameObject terrain = GameObject.Find("Terrain");
        if(terrain)
        {
            //获得宽度
            m_nMapSize = (int)terrain.GetComponent<Collider>().bounds.size.x;
        }
        else
        {
            m_nMapSize = 256;
        }
        GUILayout.Label("", GUILayout.Width(10));

        GUILayout.Label("地图大小：     "+m_nMapSize.ToString());
        if(nPreSize != m_nMapSize)
        {
            GridManager.GetInstance().SetMapSize(m_nMapSize);
        }

        m_eControlState = GUILayout.Toolbar(m_eControlState, new string[] { "无","地板","编辑墙", "编辑木墙", "编辑物件", "删除" }, GUILayout.Height(30));
        
        switch(m_eControlState)
        {
            case (int)ControlState.ecs_Ground:
            {
                groundEditor.OnGUI();
            }
            break;
            case (int)ControlState.ecs_BuildingWall:
            {
                wallEditor.OnGUI();
            }
            break;
            case (int)ControlState.ecs_BuildingWoodenWall:
            {
                woodenWallEditor.OnGUI();
            }
            break;
            case (int)ControlState.ecs_Other:
            {
                otherEditor.OnGUI();
            }
            break;
        }
        wantsMouseMove = true;
    }
    void OnDestroy()
    {
        GameObject.DestroyImmediate(selObj);
        GridManager.GetInstance().ClearGrid();
    }

    private static void OnSceneGUI(SceneView sceneview)
    {
        if(selObj != null)
        {
            switch(m_eControlState)
            {
                case (int)ControlState.ecs_Ground:
                {
                    groundEditor.OnSceneGUI(sceneview);
                }
                break;
                case (int)ControlState.ecs_BuildingWall:
                {
                    wallEditor.OnSceneGUI(sceneview);
                }
                break;
                case (int)ControlState.ecs_BuildingWoodenWall:
                {
                    woodenWallEditor.OnSceneGUI(sceneview);
                }
                break;                
                case (int)ControlState.ecs_Other:
                {
                    otherEditor.OnSceneGUI(sceneview);
                }
                break;
                case (int)ControlState.ecs_Del:
                {
                    int controlID = GUIUtility.GetControlID( FocusType.Passive);
                    if(Event.current.type == EventType.Layout)
                    {
                        HandleUtility.AddDefaultControl(controlID);
                    }
                    Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    RaycastHit hitInfo;

                    if (Physics.Raycast(worldRay, out hitInfo, 10000.0f, GridManager.mGridListLayerMask))
                    {                
                        int nIndexX = (int)hitInfo.point.x;
                        int nIndexZ = (int)hitInfo.point.z;
                        
                        if(Event.current.type == EventType.MouseMove)
                        {// 未按键移动
                            MyBuildingTool.selObj.transform.position = new Vector3((float)nIndexX+0.5f,0,(float)nIndexZ+0.5f);
                        }
                        else if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
                        {// 左键按下拖动
                            nEndX = nIndexX;
                            nEndZ = nIndexZ;
                            int nTStartX, nTEndX;
                            int nTStartZ, nTEndZ;
                            if(nStartX < nEndX)
                            {
                                nTStartX = nStartX;
                                nTEndX = nEndX;
                            }
                            else
                            {
                                nTStartX = nEndX;
                                nTEndX = nStartX;
                            }
                            if(nStartZ < nEndZ)
                            {
                                nTStartZ = nStartZ;
                                nTEndZ = nEndZ;
                            }
                            else
                            {
                                nTStartZ = nEndZ;
                                nTEndZ = nStartZ;
                            }
                            MyBuildingTool.selObj.transform.position = new Vector3((nEndX+nStartX)*0.5f+0.5f,0,(nEndZ+nStartZ)*0.5f+0.5f);
                            MyBuildingTool.selObj.transform.localScale = new Vector3(nTEndX-nTStartX+1,0.1f,nTEndZ-nTStartZ+1);
                        }
                        else if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
                        {// 点击左键
                            MyBuildingTool.selObj.transform.position = new Vector3((float)nIndexX+0.5f,0,(float)nIndexZ+0.5f);
                            MyBuildingTool.selObj.transform.localScale = new Vector3(1,0.1f,1);
                            nStartX = nIndexX;
                            nStartZ = nIndexZ;
                            nEndX = nStartX;
                            nEndZ = nStartZ;
                        }
                        else if(Event.current.type == EventType.MouseUp && Event.current.button == 0)
                        {
                            int nTStartX, nTEndX;
                            int nTStartZ, nTEndZ;
                            if(nStartX < nEndX)
                            {
                                nTStartX = nStartX;
                                nTEndX = nEndX;
                            }
                            else
                            {
                                nTStartX = nEndX;
                                nTEndX = nStartX;
                            }
                            if(nStartZ < nEndZ)
                            {
                                nTStartZ = nStartZ;
                                nTEndZ = nEndZ;
                            }
                            else
                            {
                                nTStartZ = nEndZ;
                                nTEndZ = nStartZ;
                            }
                            for(int z = nTStartZ; z < nTEndZ+1; z++)
                            {
                                for(int x = nTStartX; x < nTEndX+1; x++)
                                {
                                    GridManager.GetInstance().DelBuilding(x, z);
                                }
                            }
                            MyBuildingTool.selObj.transform.localScale = new Vector3(1,0.1f,1);
                        }
                    }
                }
                break;
                default:
                {                    
                    if(MyBuildingTool.selObj) MyBuildingTool.selObj.transform.position = new Vector3(0,0,0);
                }
                break;
            }
        }
    }
}